/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-testdriving
*
* @File name : TestDrivingService.java
*
* @Author : Administrator
*
* @Date : 2017年11月8日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年11月8日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.testdriving.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.marketing.common.dto.testdrivingAm.TestDrivingAmDto;
import org.marketing.common.dto.testdrivingAm.TestdrivingAmFrecordDto;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.icu.util.Calendar;
import com.yonyou.gmmc.common.service.BaseService;
import com.yonyou.gmmc.service.testdriving.constant.TestDrivingConstant;
import com.yonyou.gmmc.service.testdriving.dao.TtTestdriveAppointmentMapper;
import com.yonyou.gmmc.service.testdriving.entity.TestdrivingAmFrecordPO;
import com.yonyou.gmmc.service.testdriving.entity.TestdrivingAmPO;

/**
 * @author Administrator 
 * 试驾预约
 * @date 2017年11月8日
 */
@Service
public class TestDrivingService extends BaseService {

    private static Logger logger = LoggerFactory.getLogger(TestDrivingService.class);
    
    @Autowired
    private TtTestdriveAppointmentMapper ttTestdriveAppointmentMapper;
    
    /**
    *
    * @author LiuJun
    * 试驾预约保存
    * @date 2016年11月7日
    * @param testDrivingAmDto
    * @return
    */
    public TestDrivingAmDto saveTestDrivingAppointment(TestDrivingAmDto testDrivingAmDto,String openId) {
        try {
            String potentialUserId = ttTestdriveAppointmentMapper.selectPotentialByOpen(openId);
            
            TestdrivingAmPO testdrivingAmPO = new TestdrivingAmPO();
            
            testDrivingAmDto.setCreateDate(new Date());
            
            testDrivingAmDto.setAppointmentStatus(TestDrivingConstant.DRIVINGTEST_STATUS_SAVED);
            
            //设置默认来源类型
            if(CheckUtil.NullOrEmpty(testDrivingAmDto.getSourceType())){
                testDrivingAmDto.setSourceType(TestDrivingConstant.SOURCE_TYPE_WECHAT);
            }
            BeanUtils.copyProperties(testDrivingAmDto, testdrivingAmPO);
            
            testdrivingAmPO.setPotentialUserId(Long.valueOf(potentialUserId));

            ttTestdriveAppointmentMapper.insertTestDrivingAm(testdrivingAmPO);
            
            testDrivingAmDto.setTestdriveAppointmentId(testdrivingAmPO.getTestdriveAppointmentId());
            
            return testDrivingAmDto;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }

    
    /**
     * 
    *取得服务顾问的试驾预约信息
    * @author WangHuicheng
    * 
    * @date 2016年11月21日
    * @param dealerCode            经销商代码
    * @param salesConsultantId     服务顾问ID
    * @param status                状态
    * @param strStartDateFrom             预约到店时间FROM
    * @param strStartDateEnd             预约到店时间FROM
    * @return
     */
    public List<TestDrivingAmDto> getTestDriverInfo(String dealerCode,
                                                    Integer salesConsultantId,
                                                    Integer status,
                                                    String strStartDateFrom,
                                                    String strStartDateEnd){
        Date startDateFrom = null;
        Date startDateEnd = null;
        if (null != strStartDateFrom){
            startDateFrom = DateUtil.dataFormat(strStartDateFrom);
        }
        if (null != strStartDateEnd){
            startDateEnd  = DateUtil.dataFormat(strStartDateEnd);
        }
        List<TestDrivingAmDto> list = ttTestdriveAppointmentMapper.getTestDriverInfo(dealerCode, salesConsultantId, status, startDateFrom, startDateEnd);
        if (null == list){
            list = new ArrayList<TestDrivingAmDto>();
        }
        return list;
    }

        
    /**
    *
    * @author LiuJun
    * 更新试驾预约状态
    * @date 2016年11月21日
    * @param testdrivingAmFrecordDto
    */
    public void updateTestDrivingAppointStatus(TestdrivingAmFrecordDto testdrivingAmFrecordDto) {
        
        if(!isExistTestDrivingAppoint(testdrivingAmFrecordDto.getTestdriveAppointmentId())){
            throw new ServiceBizException("the record is not exist:"+testdrivingAmFrecordDto.getTestdriveAppointmentId());
        }
        try {
            
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            testdrivingAmFrecordDto.setUpdateBy(testdrivingAmFrecordDto.getUserId());
            testdrivingAmFrecordDto.setUpdateDate(calendar.getTime());
            ttTestdriveAppointmentMapper.updateTestDrivingAppointStatus(testdrivingAmFrecordDto);
            testdrivingAmFrecordDto.setCreateBy(testdrivingAmFrecordDto.getUserId());
            testdrivingAmFrecordDto.setCreateDate(calendar.getTime());
            if(testdrivingAmFrecordDto.getFollowDate() ==null){
                testdrivingAmFrecordDto.setFollowDate(calendar.getTime());
            }
            //试驾预约跟进表维护
//            TestdrivingAmFrecordPO testdrivingAmFrecordPO = new TestdrivingAmFrecordPO();
//            BeanUtils.copyProperties(testdrivingAmFrecordDto, testdrivingAmFrecordPO);
//            ttTestdriveAppointmentMapper.insertTestdrivingAmFrecord(testdrivingAmFrecordPO);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }

    }



    /**
    *
    * @author LiuJun
    * 保存试驾预约跟进记录
    * @date 2016年11月21日
    * @param testdrivingAmFrecordDto
    * @return
    */
    public TestdrivingAmFrecordDto saveTestDrivingAppointFrecord(TestdrivingAmFrecordDto testdrivingAmFrecordDto) {
        if(!isExistTestDrivingAppoint(testdrivingAmFrecordDto.getTestdriveAppointmentId())){
            throw new ServiceBizException("the record is not exist:"+testdrivingAmFrecordDto.getTestdriveAppointmentId());
        }
        try{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            
            testdrivingAmFrecordDto.setCreateBy(testdrivingAmFrecordDto.getUserId());
            testdrivingAmFrecordDto.setCreateDate(calendar.getTime());
            TestdrivingAmFrecordPO testdrivingAmFrecordPO = new TestdrivingAmFrecordPO();
            
            BeanUtils.copyProperties(testdrivingAmFrecordDto, testdrivingAmFrecordPO);
            
            
    
            ttTestdriveAppointmentMapper.insertTestdrivingAmFrecord(testdrivingAmFrecordPO);
            
            testdrivingAmFrecordDto.setTlTestdriverRecodeId(testdrivingAmFrecordPO.getTlTestdriverRecodeId());
            
            return testdrivingAmFrecordDto;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }

    }
    
    
    /**
    *
    * @author LiuJun
    * 判断是否存在试驾预约记录
    * @date 2016年11月23日
    * @param testdriveAppointmentId
    * @return
    */
    
    public boolean isExistTestDrivingAppoint(Long testdriveAppointmentId) {
        try{
            long count = ttTestdriveAppointmentMapper.queryTestDrivingAppointCount(testdriveAppointmentId);
            
            if(count>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }



    /**
    *
    * @author QianKun
    * 试驾查询列表
    * @date 2016年11月16日
    * @param potentialUserId
    * @return
    */
    public List<TestDrivingAmDto> getTestDrivingData(String openId,String phone,String submitDateFrom,String submitDateEnd,String startDateFrom,String startDateEnd,String appointmentStatus)  {
        try {
            String potentialUserId = ttTestdriveAppointmentMapper.selectPotentialByOpen(openId);
//            PageHelper.startPage(BusinessConstant.CURRENT_PAGE,BusinessConstant.PAGE_SIZE);
            List<TestDrivingAmDto> dtoList = ttTestdriveAppointmentMapper.queryTestDrivingData(potentialUserId,phone,submitDateFrom,submitDateEnd,startDateFrom,startDateEnd,appointmentStatus);          
            return dtoList;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }
    
    /**
    *
    * @author QianKun
    * 试驾查询详情
    * @date 2016年11月15日
    * @param testdriveAppointmentId
    * @return
    */
    public List<TestDrivingAmDto> getTestDrivingDataById(String testdriveAppointmentId)  {
        try {
            List<TestDrivingAmDto> dtoList = ttTestdriveAppointmentMapper.queryTestDrivingDataById(testdriveAppointmentId);
            return dtoList;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }

}
